package rmhc.gui
{
	import com.greensock.TimelineLite;
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import rmhc.design.DomikDesign;
	
	public class DomikAnim extends DomikDesign
	{
		public const area:Rectangle = new Rectangle( 44 , 169 , 158 , 196 );
		
		protected var btm:Bitmap;
		protected var mrg:MergeManager;
		
		public function DomikAnim()
		{
			super();
			domik.visible = false;
		}
		
		public function setBitmap( mrg:MergeManager ):void
		{
			this.mrg = mrg;
			mrg.mouseEnabled = mrg.mouseChildren = false;
			
			var bit:DisplayObject = mrg.getImage();
			
			var p:Point = bit.localToGlobal( new Point( 0 , 0 ) );
			var was:Point = globalToLocal( p );
			
			//var btd:BitmapData = new BitmapData( bit.width / bit.scaleX , bit.height / bit.scaleY , true , 0 );
			var btd:BitmapData = mrg.getMerged();
			//btd.draw( bit );
			
			btm = new Bitmap( btd );
			btm.smoothing = true;
			btm.width = bit.width;
			btm.height = bit.height;
			btm.x = was.x;
			btm.y = was.y;
			btm.alpha = 0;
			addChild( btm );
			trace( "btm" , btm.x , btm.y , btm.width , btm.height );
			
			new TweenLite( mrg , 3 , { alpha : 0 } );
			new TweenLite( btm , 3 , { alpha : 1 } );
		}
		public function revert():void
		{
			new TweenLite( mrg , 1 , { alpha : 1 } );
			new TweenLite( btm , 1 , { alpha : 0 , onComplete : onRevert } );
		}
		protected function onRevert():void
		{
			mrg.mouseEnabled = mrg.mouseChildren = true;
			visible = false;
		}
		 
		public function playAnim():void
		{	
			var ow:Number = btm.width / btm.scaleX;
			var oh:Number = btm.height / btm.scaleY;
			var sx:Number = area.width / ow;
			var sy:Number = area.height / oh;
			var scale:Number = sx < sy ? sx : sy;
			if ( scale > 1 )
			{
				scale = 1;
			}
			
			var pos:Point = new Point();
			var sw:Number = ow * scale;
			var sh:Number = oh * scale;
			pos.x = area.x + ( area.width - sw ) / 2;
			pos.y = area.y + ( area.height - sh ) / 2;
			trace( "domik scale" , scale , sw , sh );
			trace( "domik orig" , ow , oh );
			domik.alpha = 0;
			domik.visible = true;
			//var tml:TimelineLite = new TimelineLite();
			//new TweenLite( btm , 1.5 , { delay : 2 , scaleX : scale , scaleY : scale , x : pos.x , y : pos.y } );
			new TweenLite( btm , 1.5 , { delay : 2 , width : sw , height : sh , x : pos.x , y : pos.y } );
			new TweenLite( domik , 3 , { alpha : 1 , delay : 2.7 , onComplete : onAnim } );
		}
		protected function onAnim():void
		{
			trace( "anim complete!" );
			dispatchEvent( new Event( Event.COMPLETE ) );
		}
	}
}